/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module1;

import dao.DanhMucDAO;
import java.util.*;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import pojo.DanhMuc;
import pojo.SanPham;

/**
 *
 * @author User
 */
public class SanPhamThemDialog extends javax.swing.JDialog {

    /**
     * Creates new form SanPhamThemDialog
     */
    private SanPham info;
    public SanPhamThemDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jBtnThem = new javax.swing.JButton();
        jBtnCapNhat = new javax.swing.JButton();
        jTxtID = new javax.swing.JTextField();
        jTxtSku = new javax.swing.JTextField();
        jTxtSanPham = new javax.swing.JTextField();
        jTxtKichCo = new javax.swing.JTextField();
        jTxtSoLuong = new javax.swing.JTextField();
        jTxtMauSac = new javax.swing.JTextField();
        jTxtGiaBan = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jCbxDanhMuc = new javax.swing.JComboBox();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm Sản Phẩm");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jBtnThem.setText("Thêm");
        jBtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnThemActionPerformed(evt);
            }
        });

        jBtnCapNhat.setText("Cập Nhật");
        jBtnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCapNhatActionPerformed(evt);
            }
        });

        jTxtID.setEditable(false);

        jLabel1.setText("ID");

        jLabel2.setText("Danh Mục :");

        jLabel3.setText("Sku");

        jLabel4.setText("Tên Sản Phẩm");

        jLabel5.setText("Kích Cỡ");

        jLabel6.setText("Số Lượng");

        jLabel7.setText("Giá Bán:");

        jLabel8.setText("Màu Sắc:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jBtnThem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                        .addComponent(jBtnCapNhat))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel8))
                        .addGap(63, 63, 63)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtKichCo, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTxtSanPham, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTxtSku)
                            .addComponent(jTxtID)
                            .addComponent(jTxtSoLuong)
                            .addComponent(jTxtMauSac)
                            .addComponent(jTxtGiaBan)
                            .addComponent(jCbxDanhMuc, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(146, 146, 146))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTxtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jCbxDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTxtSku, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(46, 46, 46))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jTxtSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtKichCo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTxtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtMauSac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtGiaBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnCapNhat)
                    .addComponent(jBtnThem)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void setCbx(){
         DanhMucDAO dmDao = new DanhMucDAO();
        List ds = dmDao.findAll();
        DefaultComboBoxModel dcm=new DefaultComboBoxModel();
        
        for (int i = 0; i < ds.size(); i++) {
           DanhMuc dm=(DanhMuc)ds.get(i);
            Vector vt=new Vector();
            vt.add(dm.getId());
            vt.add(dm.getTen());
            dcm.addElement(vt);
        }
       
        this.jCbxDanhMuc.setModel(dcm);

    }
    private int getCbx(){
        Vector vt1=(Vector)this.jCbxDanhMuc.getSelectedItem();
      int maDanhMuc=(Integer)vt1.get(0);
      return maDanhMuc;
    }
    private void setForm(){
        if(String.valueOf(info.getId())!=null)
            jTxtID.setText(String.valueOf(info.getId()));
        jTxtSku.setText(info.getSku());
        jTxtSanPham.setText(info.getTenSanPham());
        if(String.valueOf(info.getKickCo())!=null)
            jTxtKichCo.setText(info.getKickCo());
        if(info.getSoLuong()!=null)
            jTxtSoLuong.setText(String.valueOf(info.getSoLuong()));
        if(info.getGiaBan()!=null)
            jTxtGiaBan.setText(String.valueOf(info.getGiaBan()));
        jTxtMauSac.setText(info.getMauSac());
    }
    private void getForm(){
        DanhMucDAO dmDao=new DanhMucDAO();
        DanhMuc dm=new DanhMuc();
        int id=getCbx();
        dm=dmDao.find(id);
        
        info.setDanhMuc(dm);
        
        if(jTxtID.getText().trim().isEmpty()==false)
               info.setId(Integer.parseInt(jTxtID.getText()));
       info.setSku(jTxtSku.getText());
       info.setTenSanPham(jTxtSanPham.getText());
       info.setKickCo(jTxtKichCo.getText().trim());
       if(jTxtSoLuong.getText().trim().isEmpty()==false)
            info.setSoLuong(Short.parseShort(jTxtSoLuong.getText()));
       if(jTxtGiaBan.getText().trim().isEmpty()==false)
            info.setGiaBan(Double.parseDouble(jTxtGiaBan.getText()));
       info.setMauSac(jTxtMauSac.getText());
    }
   
    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        setCbx();
        setForm();
    }//GEN-LAST:event_formWindowOpened

    private void jBtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnThemActionPerformed
        // TODO add your handling code here:
        try {
            info=new SanPham();
            getForm();
             this.setVisible(false);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex);
        }
        
    }//GEN-LAST:event_jBtnThemActionPerformed

    private void jBtnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCapNhatActionPerformed
        // TODO add your handling code here:
         try {
            getForm();
            this.setVisible(false);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex);
        }
    }//GEN-LAST:event_jBtnCapNhatActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SanPhamThemDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SanPhamThemDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SanPhamThemDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SanPhamThemDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SanPhamThemDialog dialog = new SanPhamThemDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCapNhat;
    private javax.swing.JButton jBtnThem;
    private javax.swing.JComboBox jCbxDanhMuc;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTxtGiaBan;
    private javax.swing.JTextField jTxtID;
    private javax.swing.JTextField jTxtKichCo;
    private javax.swing.JTextField jTxtMauSac;
    private javax.swing.JTextField jTxtSanPham;
    private javax.swing.JTextField jTxtSku;
    private javax.swing.JTextField jTxtSoLuong;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the info
     */
    public SanPham getInfo() {
        return info;
    }

    /**
     * @param info the info to set
     */
    public void setInfo(SanPham info) {
        this.info = info;
    }
}
